###1.simualtion from different distributions: ##normal distribution: rnorm(10,mean=0,sd=2) ##chi-square distribution: rchisq(100, df=2) ##exponential distribution: rexp(100, 2) ##t distribution: rt(10,df=2) ##Uniform distribution: runif(10, min=1, max=2) ##Gamma distribution: rgamma(20, shape=1, rate=2) ##Bernoulli distribution: library(Rlab) rbern(10, 0.5) ##Binomial distribution: rbinom(10, 2, prob=0.5) ##Poisson distribution: rpois(20,1) ##Multivariate normal distribution: library(MASS) mvrnorm(n=10, mu=c(1,2), Sigma=matrix(c(1,0,0,1),2,2)) ##Simple random samples: sample(1:10, 2, replace = FALSE) sample(1:10, 5, replace = TRUE) ##Example 1: One sample problem: ##mod=1 for normal distribution, mod=2 for chi-square distribution ##B: Monte Carlo sample size ##n: sample size for each Monte Carlo sample fsim1<-function(mod,B,n){ Etheta<-NULL EV<-NULL EsI<-NULL EL<-NULL iter<-0 repeat{ iter<-iter+1 if(mod==1){ Y<-rnorm(n,mean=1,sd=1) theta0<-1 } else if(mod==2){ Y<-rchisq(n,df=2) theta0<-2 } etheta<-mean(Y) ev<-1/n*var(Y) LB<-etheta-1.96*sqrt(ev) UB<-etheta+1.96*sqrt(ev) sI<-as.numeric(theta0>LB & theta0LB & theta0LB & theta0LB1 & theta0LB2 & theta0 fsim4(1000,100,100) BIAS SE MSE RB RSE RRMSE (Intercept) 0.02586266 0.2597998 0.06816481 0.02586266 0.2597998 0.2610839 X 0.05523762 0.2972840 0.09142899 0.05523762 0.2972840 0.3023723 VRB1 CR1 AL1 VRB2 CR2 AL2 (Intercept) 0.003757506 0.962 1.015219 0.1487553 0.967 1.077950 X -0.005273640 0.960 1.150720 0.1843197 0.967 1.241654